// pages/Bonus/withdraw/withdraw.js
var util = require('../../../utils/util.js')
var app = getApp()
var flag = false
var allmoney
var warn
var flags = true
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addBankCard: false,
    withdrawBankCard:true,
    infoid:'',
    bankId:'',
    tmoney:'',
    ktxmoney:0,
    banktitle: '',
    endno: '0000',
    bankno: '0000',
    name:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取我的可提现金额
    this.getMymoney()
    this.getBingBank()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	app.publicchangenavigate()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var ists = wx.getStorageSync('bankist')
    if (ists == 1) {
      this.getBingBank()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
	return app.globalshare();
  },
  //添加银行卡
  addBankCardTap:function(){
	  app.publicnavigateTologing('/pages/Bonus/addBankCard/addBankCard')
  },
  //修改银行卡
  changeBankCardTap:function(e){
    var infoid = e.currentTarget.dataset.infoid;
    app.publicnavigateTologing('/pages/Bonus/addBankCard/addBankCard?infoid=' + infoid)
  },
  //提现金额
  bindusnInput: function (e) {
    this.setData({
      tmoney: e.detail.value,
    })
  },
  //获取我的可提现金额
  getMymoney: function () {
    var that = this
    var uid = app.globalData.userInfoId
    //开启加载
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    //提交给服务端
    util.AJAX('Home/Troops/getMymoney', { uid: uid }, function (res) { //方法体
      //关闭加载
      wx.hideLoading()

      if (res.data.StatusCode == 200) {  //成功
        allmoney = res.data.Moneys
        that.setData({ ktxmoney: res.data.Moneys })
      } else {	//失败

      }
    }, 'POST')
  },
  //全部提现
  bindTxtap:function(){
    this.setData({
      tmoney: allmoney
    })
  },
  //确认提现
  bindqrtx:function(){
    var that = this
    var money = this.data.tmoney
    var bankno = this.data.bankno
    var bankId = this.data.bankId
    var name = this.data.name
    var btitle = this.data.banktitle
    if (bankno=="0000"){
      warn = "请先绑定银行卡！";
    }else if (money<=0){
      warn = "请输入提现金额！";
    } else if (money > allmoney) {
      warn = "可提现金额不足！";
    } else if (money <= 1){
      warn = "提现手续费最低1元！";
    } else{
      flag = true
    }
    if(flag){
      if(flags){
        flags = false
        //计算手续费
        var feemoney = (money/1000);
        if(feemoney>1 && feemoney<25){
          feemoney = Math.round(feemoney * 100) / 100
        }else if(feemoney<=1){
          feemoney = 1
        }else{
          feemoney = 25
        }
        var sjmoney = money-feemoney
        wx.showModal({
          title: '提示',
          content: '本次提现金额为' + money + '元,手续费为' + feemoney + '元,实际到账金额为' + sjmoney + '元,是否提现？',
          success: function (res) {
            if (res.confirm) {
              var uid = app.globalData.userInfoId
              //var uid = 2
              //开启加载
              wx.showLoading({
                title: '加载中',
                mask: true,
              })
              //提交给服务端
              util.AJAX('Home/Troops/withdraw', { uid: uid, money: money, bankno: bankno, bankId, bankId,name:name,banktitle:btitle }, function (res) { //方法体
                //关闭加载
                wx.hideLoading()
                
                if (res.data.StatusCode == 200) {  //成功
                  //存入状态
                  wx.setStorageSync("txist", 1);

                  var money_t = allmoney-money
                  allmoney = money_t
                  that.setData({
                    ktxmoney:money_t,
                    tmoney:''
                  })
                  flag = false
                  wx.showToast({
                    title: res.data.Msg,
                    icon: 'success',
                    duration: 2000
                  })
                  flags = true
                  var endno = that.data.endno
                  var banktitle = that.data.banktitle
                  app.publicnavigateTologing('/pages/Bonus/withdrawSuccess/withdrawSuccess?bankendno=' + endno + '&money=' + money + '&banktitle=' + banktitle)
                } else {	//失败
                  flag = false
                  wx.showModal({
                    title: '提示',
                    content: res.data.Msg
                  })
                  flags = true
                }
              }, 'POST')
            } else if (res.cancel) {
              flags = true
            }
          }
        })
      }
    }else{
      wx.showModal({
        title: '提示',
        content: warn
      })
    }
  },
  /**
   * 获取绑定的银行卡
   */
  getBingBank:function(){
    var uid = app.globalData.userInfoId
    var that = this
    //开启加载
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    //提交给服务端
    util.AJAX('Home/Troops/getBingBank', { uid: uid }, function (res) { //方法体
      //关闭加载
      wx.hideLoading()
      if (res.data.StatusCode == 200) {  //成功
        that.setData({ banktitle: res.data.BankTitle, bankId: res.data.BankId, endno: res.data.EndNo, addBankCard: false, withdrawBankCard: true, bankno: res.data.Bankno, name: res.data.name, infoid: res.data.Infoid })
      } else {	//失败
        that.setData({ addBankCard: true, withdrawBankCard: false })
      }
    }, 'POST')
  }
})